var os = require('os');
var Webpack = require('webpack');
// var HappyPack = require('happypack');
var HtmlWebapckPlugin = require('html-webpack-plugin');
var ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
var UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
    entry: {
        main: './src/main.js',
        vendor: ['react', 'react-dom','redux']
    },
    output: {
        path: __dirname + '/dist',
        filename: 'js/[name].[hash].js',
        chunkFilename: 'js/[name].[chunkhash].js',
        publicPath: '/'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                  loader: 'babel-loader',
                  options: {
                    presets: ['env', 'stage-0', 'react'],
                    plugins: [['import', {"libraryName": "antd", "style": "css"}]]
                  }
                }
            },
            { 
                test: /\.tsx?$/, 
                loader: [
                    "awesome-typescript-loader"
                ],
                include: __dirname + '/src' 
            },
            { 
                enforce: "pre", 
                test: /\.js$/, 
                loader: "source-map-loader" 
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.less$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'less-loader'
                ]
            }
        ]
    },
    resolve: {
        extensions: ['.ts','.tsx','.js', '.jsx', 'json'],
        modules: [
            'src',
            'node_modules'
        ],
        alias: {
            $components: __dirname + '/src/components',
            $layout: __dirname + '/src/components/layout',
            $router: __dirname + '/src/router',
            $actions: __dirname + '/src/actions',
            $reducers: __dirname + '/src/reducers',
            $store: __dirname + '/src/store'
        }
    },
    externals: {
        'React': 'react',
        'antd': 'antd'
    },
    plugins: [
        new Webpack.HotModuleReplacementPlugin(),
        new UglifyJsPlugin({
            test: /\.js(\?.*)?$/i,
            include: /\/includes/,
            exclude: /\/src/,
            parallel: 6 || 4,
            sourceMap: true
        }),
        new Webpack.ProvidePlugin({
            "React": "react",
        }),
        new HtmlWebapckPlugin({
            title: '主页',
            inject: 'body',
            template: './src/index.html'
        }),
    ],
    optimization: {
        splitChunks: {
            cacheGroups: {
                commons: {
                    name: "vendor",
                    chunks: "initial",
                    minChunks: 2
                }
            }
        }
    }
}